import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prj.ufdm.demo.util.UfdmFileUtil;
import com.prj.ufdm.demo.util.UfdmRequestHttpUtil;

public class Test {

	public static void main(String[] args) throws Exception {
		
		String fileName = "bloc.abc";
		
		System.out.println(fileName.lastIndexOf("."));
		
		System.out.println(fileName.lastIndexOf(".")!=-1 ? fileName.substring(0,fileName.lastIndexOf(".")) : fileName);
		
		
		
		/**
		String filePath = "D:\\newdown\\jdk1.8.0_20_i586.zip";
		int chunkSize = 30 * 1024 * 1024;
		File file = new File(filePath);
//		UfdmFileUtil.splitFile(file, chunkSize ,"part");
		String [] upFiles = new String [] {
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000001.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000002.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000003.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000004.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000005.part"
			};
		// String [] upFiles = new String [] { "D:\\newdown\\test_doc.docx" };
		// UfdmFileUtil.combineFile(file,"part");
		
		System.out.println("\n 开始获取wholeFileId:");
		
		String wholeFileId = getWholeFileIdByWholeFile(file,"别名",String.valueOf(chunkSize));
//		String wholeFileId = "5d3fe2391acb6223fc7fb899";
		
		
		System.out.println("\n wholeFileId:"+wholeFileId);
		
		if(!wholeFileId.equals("-1")) {
		    for(int i = 0; i < upFiles.length; i++) {
				String reqUrl = "http://localhost:5204/api/file/upload";
				Map<String,String> reqParamMap = new HashMap<String,String>();
				reqParamMap.put("wholeFileId", wholeFileId);
				reqParamMap.put("chunkNum", String.valueOf(i));
				reqParamMap.put("aliases", "测试别名");
				Map<String, File> reqFileMap = new HashMap<String, File>();
				reqFileMap.put("file", new File(upFiles[i]));
				System.out.println(UfdmRequestHttpUtil.postFile(reqUrl,null,reqParamMap,reqFileMap,"utf-8"));
		    }
		}
		*/
	}
	
	
	public static String getWholeFileIdByParam(File file) throws Exception {
		String wholeFileId = "-1";
		String encode = "utf-8";
		String reqUrl = "http://localhost:5204/api/file/crewfid";
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("aliases", "JDK1.8安装包");
		reqParamMap.put("fileHash", DigestUtils.md5Hex(new FileInputStream(file)));
		reqParamMap.put("fileSize", String.valueOf(file.length()));
		reqParamMap.put("fileType", file.getName().substring(file.getName().lastIndexOf(".") + 1));
		reqParamMap.put("chunkCount", "5");
		String getWholeFileIdResult = UfdmRequestHttpUtil.postParam(reqUrl,reqHeadMap,reqParamMap,encode);
		JSONObject retResult = JSON.parseObject(getWholeFileIdResult);
		System.out.println(retResult);
		JSONObject retHead = (JSONObject) retResult.get("retHead");
		if(retHead.getString("code").equals("0000000")) {
			JSONObject retBody = (JSONObject) retResult.get("retBody");
			wholeFileId = retBody.getString("id");
		}
		System.out.println(wholeFileId);
		return wholeFileId;
	}
	
	
	public static String getWholeFileIdByWholeFile(File file,String aliases,String chunkSize) throws Exception {
		String wholeFileId = "-1";
		String encode = "utf-8";
		String reqUrl = "http://localhost:5204/api/file/crewfidbwf";
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("chunkSize", chunkSize);
		reqParamMap.put("aliases", aliases);
		Map<String, File> reqFileMap = new HashMap<String, File>();
		reqFileMap.put("file", file);
		String getWholeFileIdResult = UfdmRequestHttpUtil.postFile(reqUrl, reqHeadMap, reqParamMap, reqFileMap, encode);
		JSONObject retResult = JSON.parseObject(getWholeFileIdResult);
		System.out.println(retResult);
		JSONObject retHead = (JSONObject) retResult.get("retHead");
		if(retHead.getString("code").equals("0000000")) {
			JSONObject retBody = (JSONObject) retResult.get("retBody");
			wholeFileId = retBody.getString("id");
		}
		System.out.println(wholeFileId);
		return wholeFileId;
	}

}
